﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.Odbc;
using Cat;
using MyDBconnection;

namespace Thesis_2
{
    public partial class frmProductList : Form
    {
        
        DatabaseConnection dbconn = new DatabaseConnection();
        public frmProductList()
        {
            InitializeComponent();
            this.Load += new EventHandler(frmProductList_Load);
            dataGridView1.RowPostPaint += new DataGridViewRowPostPaintEventHandler(dataGridView1_RowPostPaint);
           
            txtProductID.TextChanged += new EventHandler(txtProductID_TextChanged);
            txtProductName.TextChanged += new EventHandler(txtProductName_TextChanged);
            linkLabel1.Click += new EventHandler(linkLabel1_Click);     
        }
        void linkLabel1_Click(object sender, EventArgs e)
        {
            frmproduct frmP = new frmproduct();
            frmP.Show();
            this.Close();
        }
        void txtProductName_TextChanged(object sender, EventArgs e)
        {
            if (txtProductName.Text != "Search Product Name")
            {
                dbconn.dbconnection();
                dbconn.loadData("SELECT  * FROM tblproduct WHERE ProductName LIKE '%" + txtProductName.Text + "%'");
                dataGridView1.DataSource = dbconn.tbl;
                dbconn.DBClose();
            }
            else
            {
                GetData();
            }
        }
        void txtProductID_TextChanged(object sender, EventArgs e)
        {
            if (txtProductID.Text != "Search ProductID")
            {
                dbconn.dbconnection();
                dbconn.loadData("SELECT  * FROM tblproduct WHERE ProductID LIKE '%" + txtProductID.Text + "%'");
                dataGridView1.DataSource = dbconn.tbl;
                dbconn.DBClose();
            }
            else
            {

                GetData();

            }
        }
        void dataGridView1_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {
            string strRowNumber = (e.RowIndex + 1).ToString();
            SizeF size = e.Graphics.MeasureString(strRowNumber, this.Font);
            if (dataGridView1.RowHeadersWidth < Convert.ToInt32((size.Width + 100)))
            {
                dataGridView1.RowHeadersWidth = Convert.ToInt32((size.Width + 100));
            }
            Brush b = SystemBrushes.ControlText;
            e.Graphics.DrawString(strRowNumber, this.Font, b, e.RowBounds.Location.X + 15, e.RowBounds.Location.Y + ((e.RowBounds.Height - size.Height) / 2));
        }
        void frmProductList_Load(object sender, EventArgs e)
        {
            GetData();
            dataGridView1.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCells;
            dataGridView1.AutoSizeColumnsMode =  DataGridViewAutoSizeColumnsMode.AllCells;
            txtProductID.Focus();
        }
        public void GetData()
        {
            dbconn.dbconnection();
            dbconn.loadData("Select * from tblproduct");
            dataGridView1.DataSource = dbconn.tbl;
            dbconn.DBClose();
           
        }
    }
}
